unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, StdCtrls, IniFiles, HTTPSend;

type

  { TCEOOnlineUpdate }

  TCEOOnlineUpdate = class
  private
    FIniLocal, FIniRemote: TIniFile;
    FHTTP: THTTPSend;

    procedure CopyLocalToRemote;
    procedure CopyRemoteToLocal;
  public
    constructor Create;
    destructor Destroy; override;

    property LocalFile: TIniFile read FIniLocal write FIniLocal;
    property RemoteFile: TIniFile read FIniRemote write FIniRemote;
  end;

  { TfrmMain }

  TfrmMain = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    FCEOOnlineUpdate: TCEOOnlineUpdate;
  public
    { public declarations }
  end;


var
  frmMain: TfrmMain;

implementation

const
  cContentVersionURL = 'http://ceo-companion.googlecode.com/hg/content/ceo_version.ini';
  envAppContentDir = '/home/gcarreno/.config/ceo-companion/content';
  envAppOnlineUpdateFile = '/home/gcarreno/.config/ceo-companion/content/ceo_version.ini';

{ TCEOOnlineUpdate }

procedure TCEOOnlineUpdate.CopyLocalToRemote;
var
  Sections, Idents: TStringList;
  SectionIndex, IdentIndex: Integer;
begin
  Sections:= TStringList.Create;
  Idents:= TStringList.Create;
  try
    FIniLocal.ReadSections(Sections);
    for SectionIndex:= 0 to Sections.Count - 1 do
    begin
      FIniLocal.ReadSection(Sections[SectionIndex], Idents);
      for IdentIndex:=0 to Idents.Count - 1 do
      begin
        FIniRemote.WriteString(Sections[SectionIndex], Idents[IdentIndex],
          FIniLocal.ReadString(Sections[SectionIndex], Idents[IdentIndex], ''));
      end;
    end;
  finally
    Sections.Free;
    Idents.Free;
  end;
end;

procedure TCEOOnlineUpdate.CopyRemoteToLocal;
var
  Sections, Idents: TStringList;
  SectionIndex, IdentIndex: Integer;
begin
  Sections:= TStringList.Create;
  Idents:= TStringList.Create;
  try
    FIniRemote.ReadSections(Sections);
    for SectionIndex:= 0 to Sections.Count - 1 do
    begin
      FIniRemote.ReadSection(Sections[SectionIndex], Idents);
      for IdentIndex:=0 to Idents.Count - 1 do
      begin
        FIniLocal.WriteString(Sections[SectionIndex], Idents[IdentIndex],
          FIniRemote.ReadString(Sections[SectionIndex], Idents[IdentIndex], ''));
      end;
    end;
  finally
    Sections.Free;
    Idents.Free;
  end;
end;

constructor TCEOOnlineUpdate.Create;
begin
  inherited Create;

  if not DirectoryExists(envAppContentDir) then
  begin
    CreateDir(envAppContentDir);
  end;
  FHTTP:= THTTPSend.Create;
  try
    if FHTTP.HTTPMethod('GET', cContentVersionURL) then
    begin
      if FHTTP.ResultCode = 200 then
      begin
        FIniRemote:= TIniFile.Create(FHTTP.Document);
      end
      else
      begin
        FIniRemote:= TIniFile.Create(TMemoryStream.Create);
        CopyLocalToRemote;
      end;
    end;
  finally
    FHTTP.Free;
  end;
  if not FileExists(envAppOnlineUpdateFile) then
  begin
    FIniLocal:= TIniFile.Create(envAppOnlineUpdateFile);
    CopyRemoteToLocal;
  end
  else
  begin
    FIniLocal:= TIniFile.Create(envAppOnlineUpdateFile);
  end;
end;

destructor TCEOOnlineUpdate.Destroy;
begin
  if Assigned(FIniLocal) then
    FIniLocal.Free;
  if Assigned(FIniRemote) then
    FIniRemote.Free;

  inherited Destroy;
end;

{$R *.lfm}

{ TfrmMain }

procedure TfrmMain.Button1Click(Sender: TObject);
var
  slLines: TStringList;
begin
  slLines:= TStringList.Create;
  try
    FCEOOnlineUpdate.LocalFile.ReadSectionRaw('Main', slLines);
    Memo1.Lines.Add('Local:');
    Memo1.Lines.Add(slLines.Text);
    FCEOOnlineUpdate.RemoteFile.ReadSectionRaw('Main', slLines);
    Memo1.Lines.Add('Remote:');
    Memo1.Lines.Add(slLines.Text);
  finally
    slLines.Free;
  end;
end;

procedure TfrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  FCEOOnlineUpdate.Free;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FCEOOnlineUpdate:= TCEOOnlineUpdate.Create;
end;

end.

